package com.ruoyi.system.controller;

import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysConfig;
import com.ruoyi.system.service.ISysOrgConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 机构参数配置 信息操作处理
 *
 * @author ruoyi
 */
@Api(tags = "机构参数配置")
@RestController
@RequestMapping("/org/config")
public class SysOrgConfigController extends BaseController {

    @Autowired
    private ISysOrgConfigService orgConfigService;

    /**
     * 获取参数配置列表
     */
    @RequiresPermissions("system:orgConfig:list")
    @GetMapping("/list")
    @ApiOperation(value = "获取参数配置列表", response = SysConfig.class)
    public TableDataInfo list(SysConfig config) {
        startPage();
        List<SysConfig> list = orgConfigService.selectConfigList(config);
        return getDataTable(list);
    }

    /**
     * 获取参数配置列表
     */
    @InnerAuth
    @PostMapping("/configList")
    @ApiOperation(value = "获取参数配置列表", response = SysConfig.class)
    public AjaxResult configList(@RequestBody SysConfig config) {
        List<SysConfig> list = orgConfigService.selectConfigList(config);
        return success(list);
    }

    @Log(title = "导出参数配置列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出参数配置列表")
    public void export(HttpServletResponse response, SysConfig config) {
        List<SysConfig> list = orgConfigService.selectConfigList(config);
        ExcelUtil<SysConfig> util = new ExcelUtil<SysConfig>(SysConfig.class);
        util.exportExcel(response, list, "参数数据");
    }

    /**
     * 根据参数编号获取详细信息
     */
    @GetMapping(value = "/{configId}")
    @ApiOperation(value = "获取参数配置列表", response = SysConfig.class)
    public AjaxResult getInfo(@ApiParam(value = "参数键值id", required = true) @PathVariable Long configId) {
        return success(orgConfigService.selectConfigById(configId));
    }

    /**
     * 根据参数键名查询参数值
     */
    @GetMapping(value = "/configKey/{configKey}")
    @ApiOperation(value = "根据参数键名查询参数值", response = SysConfig.class)
    public AjaxResult getConfigKey(@ApiParam(value = "参数键值", required = true) @PathVariable String configKey) {
        return success(orgConfigService.selectConfigByKey(configKey));
    }

    /**
     * 新增参数配置
     */
    @Log(title = "新增参数配置", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增参数配置")
    public AjaxResult add(@Validated @RequestBody SysConfig config) {
        if (UserConstants.NOT_UNIQUE.equals(orgConfigService.checkConfigKeyUnique(config))) {
            return error("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setCreateBy(SecurityUtils.getUsername());
        return toAjax(orgConfigService.insertConfig(config));
    }

    /**
     * 修改参数配置
     */
    @Log(title = "修改参数配置", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改参数配置")
    public AjaxResult edit(@Validated @RequestBody SysConfig config) {
        if (UserConstants.NOT_UNIQUE.equals(orgConfigService.checkConfigKeyUnique(config))) {
            return error("修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(orgConfigService.updateConfig(config));
    }

    /**
     * 删除参数配置
     */
    @Log(title = "删除参数配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{configIds}")
    @ApiOperation(value = "删除参数配置")
    public AjaxResult remove(@ApiParam(value = "参数配置id", required = true) @PathVariable Long[] configIds) {
        orgConfigService.deleteConfigByIds(configIds);
        return success();
    }

    /**
     * 刷新参数缓存
     */
    // @RequiresPermissions("system:orgConfig:remove")
    @Log(title = "刷新参数缓存", businessType = BusinessType.CLEAN)
    @DeleteMapping("/delete/refreshCache")
    @ApiOperation("刷新参数缓存")
    public AjaxResult refreshCache() {
        orgConfigService.resetConfigCache();
        return success();
    }
}
